home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 13.0 KB | 547 lines | [TEXT/MPS ] |
- /*
- File: StatusMonitor.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #pragma segment StatusMonitor
-
- #ifndef __STATUSMONITOR__
- #include "StatusMonitor.h"
- #endif
-
- #ifndef __UAPPLEEVENTS__
- #include "UAppleEvents.h"
- #endif
-
- #ifndef __STATUSDATABASE__
- #include "StatusDatabase.h"
- #endif
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #ifndef __OBJECTLIST__
- #include "ObjectList.h"
- #endif
-
- #ifndef __MAILGATEWAY__
- #include "MailGateway.h"
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
- /***********************************|****************************************/
-
- ImplementList(ABuffer,TBufferList,true);
- ImplementList(TSession,TSessionList,true);
-
- /***********************************|****************************************/
-
- #pragma segment Main
-
- /***********************************|****************************************/
-
- Boolean GetStandardStatusItem ( const ATupleKey& key, ADataItem& item );
-
- /***********************************|****************************************/
-
- void DoUpdateMonitor (unsigned long g, unsigned long)
- {
- TSetRefCon ('MONI');
- if ( g )
- ((TStatusMonitor*) g)->UpdateMonitor();
- }
-
- /***********************************|****************************************/
-
- #pragma segment StatusMonitor
-
- /***********************************|****************************************/
-
- TStatusMonitor::TStatusMonitor(TMailGateway* gateway):
- TDirectObject (),
- fMonitorThread ( nil ),
- fSessionList (),
- fMailGateway ( gateway )
- {
- }
-
- /***********************************|****************************************/
-
- TStatusMonitor::~TStatusMonitor()
- {
- }
-
- /***********************************|****************************************/
-
- void TStatusMonitor::UpdateLog(long infoID, char* theMsg)
- {
- unsigned long numSessions = fSessionList.Count ();
-
- for ( unsigned long i = 1; i <= numSessions; i++ )
- {
- TSession* theSession = fSessionList.Get(i);
- long sessionID = theSession->GetSessionID();
- unsigned long items = theSession->CountItems();
-
- for (unsigned long index = 1; index <= items; index++)
- {
- long id = theSession->GetItemValue(index);
-
- if ( id == infoID )
- {
- TAppleEvent theMessage ('stmn','updt',theSession->GetAddress(),kAENoReply);
- theMessage.WriteLong('SSID',theSession->GetSessionID());
- theMessage.WriteParameter(id, typeWildCard,theMsg,strlen(theMsg));
- OSErr temp = noErr;
- theMessage.Send(temp);
- break;
- }
- }
- }
- }
-
- /***********************************|****************************************/
-
- OSErr TStatusMonitor::Run()
- {
- fMonitorThread = createThread ('MONI', (FNULUL) DoUpdateMonitor, 24000, (unsigned long) this, 0, TNormalMode);
- return noErr;
- }
-
- /***********************************|****************************************/
-
- TSession* TStatusMonitor::FindMatchingSession(const TAddressDescription& theAddress, long docSessionID) const
- {
- if ( theAddress.fData.dataHandle )
- {
- long ppcIDA = (*((TargetIDHdl) (theAddress.fData.dataHandle)))->sessionID;
- unsigned long count = fSessionList.Count();
-
- for ( unsigned long i = 1; i <= count; i++)
- {
- TSession* theSession = fSessionList.Get(i);
- TAddressDescription sessionAddress = theSession->GetAddress ();
-
- long ppcIDB = (*((TargetIDHdl) (sessionAddress.fData.dataHandle)))->sessionID;
-
- if ((docSessionID == theSession->GetSessionID()) && (ppcIDA == ppcIDB))
- return theSession;
- }
- }
- else
- steve << "Why is the FindMatchingSession theAddress.fData.dataHandle nil ? " << endl;
-
- return nil;
- }
-
- /***********************************|****************************************/
-
- void TStatusMonitor::HandleAppleEvents ( const AppleEvent& message, const AppleEvent& reply, long info )
- {
- TAppleEvent theMessage ( message );
- // TAddressDescription theAddress;
- long sessionID;
- TSession* theSession;
-
- AEAddressDesc address;
- theMessage.GetAddress(address);
-
- TAddressDescription theAddress ( address );
- theMessage.ReadLong('SSID', sessionID);
-
- switch (info)
- {
- case cStatusLogInit:
- {
- TAppleEvent theReply (reply);
-
- steveF ( 3, "Got cStatusLogInit at " << time << " Session ID " << sessionID );
-
- fSessionList.Delete ( FindMatchingSession ( theAddress,sessionID) );
- TSession* newSession = new TSession(theAddress,sessionID);
- fSessionList.Append(newSession);
-
- theReply.WriteLong('SUPT',cSupportMonitoring);
- OSErr err = noErr;
- theReply.Send(err);
- }
- break;
-
- case cItemsToBeMonitored:
- {
- steveF ( 3, "Got cItemsToBeMonitored at " << time << " Session ID " << sessionID );
-
- TBufferList* list = new TBufferList;
- theMessage.ReadList('INFO', typeLongInteger, *list);
- theSession = FindMatchingSession ( theAddress,sessionID);
-
- #if debug
- if (steveFlag.Flag(3)) {
- for (unsigned long l = 1; l <= list->Count(); ++l) {
- ABuffer* buffer = list->Get(l);
- if (buffer)
- OutputOSType (steve, * (OSType*) buffer->GetPhysicalStart());
- steve << " ";
- }
- }
- #endif
-
- if ( theSession )
- theSession->SetItemValue ( list );
- else
- delete list;
-
- fMonitorThread->Wake ();
- }
- break;
-
- case cStopMonitoring:
- {
- steveF ( 3, "Got cStopMonitoring at " << time << " Session ID " << sessionID );
-
- fSessionList.Delete ( FindMatchingSession ( theAddress,sessionID) );
- }
- break;
-
- case cStatusSessionAlive:
- {
- steveF ( 3, "Got cStatusSessionAlive at " << time << " Session ID " << sessionID );
-
- theSession = FindMatchingSession ( theAddress,sessionID);
-
- if (theSession != nil)
- theSession->ResetTimer();
- }
- break;
-
- case cGetConfigCommand:
- {
- TAppleEvent theReply (reply);
- char s[64];
-
- /* What to do here is */
- /* 1. Call theMessage.ReadXXXX('WFIG', sometype, somePtr)
- to determine which configuration string is needed
- 2. Retrieve the configuration parameter from where ever
- if you don't have it return a null string
- 3. Call theReply.WriteParameter ( 'FIGV', sometype, somePtr,length of somePtr);
- 4. Call theReply.Send(err);
- */
-
- #if 1
- Str255 itemName;
-
- theMessage.ReadParameter ( 'WFIG', itemName );
-
- if ( itemName[0] )
- { CTupleKey configItem ( itemName );
- CDataItem value;
-
- steveF ( 3, "TStatusMonitor::Get Config Item '" << itemName << "'" );
-
- fMailGateway->GetConfigItem ( configItem, value );
-
- theReply.WriteParameter ( 'FIGV', value.GetDataType(), value.GetPhysicalStart(), value.GetPhysicalLength() );
- }
-
- OSErr err = noErr;
- theReply.Send ( err );
-
- #else
-
- /* This code is just here to make it work*/
- strcpy(s,"Hello");
-
- theReply.WriteParameter ( 'FIGV', 'TEXT', s,strlen(s));
- OSErr err = noErr;
- theReply.Send(err);
-
- #endif
-
- }
- break;
-
- case cSetConfigCommand:
- {
- TAppleEvent theReply (reply);
- char s[64];
-
- /* What to do here is */
- /* 1. Call theMessage.ReadXXXX('WFIG', sometype, somePtr)
- to determine which configuration string is to be set
- 2. Call theMessage.ReadXXXX('FIGV', sometype, somePtr)
- to get the value for the configuration parameter
- 3. Validate the Parameter -
- If it is OK then
- somePtr points to a NULL string
- If not then somePtr point to an error message string
- 4. Call theReply.WriteParameter ( 'FIGR', sometype, somePtr,length of somePtr);
- 5. Call theReply.Send(err);
- */
-
- #if 1
- Boolean ok = false;
-
- Str255 itemName;
- if ( theMessage.ReadParameter ( 'WFIG', itemName ) == noErr )
- { CTupleKey key ( itemName );
- CDataItem value;
-
- if ( theMessage.ReadParameter ( 'FIGV', value ) == noErr )
- {
- steveF ( 3, "TStatusMonitor::Set config item '" << itemName << "' to " << value );
-
- if ( fMailGateway->SetConfigItem ( key, value ) )
- ok = true;
- }
- }
-
- if ( ! ok )
- { CStr255 replyText;
-
- replyText = "\pThe attempt to set this parameter failed.";
-
- theReply.WriteParameter ( 'FIGR', 'TEXT', & replyText[1], replyText[0] );
- }
-
- OSErr err = noErr;
- theReply.Send ( err );
-
- #else
-
- /* This code is just here to make it work*/
- strcpy(s,"");
-
- theReply.WriteParameter ( 'FIGR', 'TEXT', s,strlen(s));
- OSErr err = noErr;
- theReply.Send(err);
-
- #endif
-
- }
- break;
-
- case cMonitorCommand:
- {
- steveF ( 3, "Got cMonitorCommand at " << time << " Session ID " << sessionID );
-
- theSession = FindMatchingSession ( theAddress,sessionID);
-
- /* What to do here is */
- /* 1. Call theMessage.ReadLong('MCMD', somePtr)
- to determine which command
- somePtr is a OSType
- 2. Do whatever the command make you do
- 3 If you want the button to have a new title
- then set the text for that status item and force and update now
- by calling UpdateMonitor
- 4. No reply is needed
-
- */
-
- CDataItem event;
-
- if ( theMessage.ReadParameter ( 'MCMD', event ) == noErr )
- {
- if ( ! fMailGateway->HandleMonitoringEvent ( event ) )
- {
- steveF ( 3, "TStatusMonitor::Event " << event << " unhandled by fMGwy->HandleMonitoringEvent()" );
- }
- }
- }
- break;
-
- default:
- steveF ( 3, "AppleEvent not handled by StatusMonitor HandleAppleEvents!! " << info );
-
- break;
- }
- }
-
- /***********************************|****************************************/
- /***********************************|****************************************/
-
- TSession::TSession ( const TAddressDescription& theAddress, long theSessionID ):
- THandleObject (),
- fAddress ( theAddress ),
- fSessionID ( theSessionID ),
- fMonitorList ( nil )
- {
- ResetTimer();
- }
-
- /***********************************|****************************************/
-
- TSession::~TSession()
- {
- delete fMonitorList;
- }
-
- /***********************************|****************************************/
-
- void TSession::SetItemValue ( TBufferList* list )
- {
- delete fMonitorList;
- fMonitorList = list;
- }
-
- /***********************************|****************************************/
-
- Boolean TSession::HasTimerExpired() const
- {
- unsigned long secs;
- GetDateTime(&secs);
- return secs - fPeriodicCheckSessionAlive > 10 * 60;
- }
-
- /***********************************|****************************************/
-
- void TSession::ResetTimer()
- {
- unsigned long secs;
- GetDateTime(&secs);
- fPeriodicCheckSessionAlive = secs;
- }
-
- /***********************************|****************************************/
-
- long TSession::GetItemValue ( unsigned long index ) const
- {
- long theID = 0;
-
- if ( fMonitorList )
- {
- const ABuffer* buffer = fMonitorList->Get ( index );
-
- if ( buffer )
- theID = *(long*) buffer->GetPhysicalStart ();
- }
-
- return theID;
- }
-
- /***********************************|****************************************/
-
- /***********************************|****************************************/
-
- Boolean GetStandardStatusItem ( const ATupleKey& key, ADataItem& item )
- { Boolean result = false;
-
- if ( key.GetLength () == sizeof(OSType) )
- {
- switch ( * (OSType *) key.GetData() )
- {
- case 'TIME': // Current time (for gateway)
- item = NowDateTime();
- result = true;
- break;
-
- case 'FMEM': // Free memory
- item = FreeMem();
- result = true;
- break;
-
- case 'DSPC': // Disk Free space
- {
- Str31 volName;
- short vRefNum;
- long freeBytes;
-
- if ( GetVInfo ( -1, (StringPtr) &volName, &vRefNum, &freeBytes) == noErr )
- item = freeBytes;
- result = true;
- break;
- }
- }
- }
-
- return result;
- }
-
-
- /***********************************|****************************************/
-
- void TStatusMonitor::UpdateMonitor()
- {
- OSErr err;
-
- while ( true )
- {
- // Update the up time flag
-
- unsigned long index = fSessionList.Count ();
-
- while ( index > 0 )
- {
- TSession* theSession = fSessionList.Get ( index );
- long sessionID = theSession->GetSessionID ();
-
- if ( theSession->HasTimerExpired () )
- {
- steveF ( 3, "Session Timed out -- killing it at " << time << " SessionID " << sessionID );
-
- fSessionList.Delete ( theSession );
- }
- else
- {
- steveF( 3, "Updating session #" << sessionID << " at " << time );
-
- TAppleEvent theMessage ('stmn','updt',theSession->GetAddress(),kAENoReply);
- theMessage.WriteLong('SSID',theSession->GetSessionID());
- unsigned long items = theSession->CountItems();
-
- for ( unsigned long jndex = 1; jndex <= items; jndex++ )
- {
- long id = theSession->GetItemValue ( jndex );
-
- CFourByteKey key ( id );
- CDataItem data;
-
- Boolean result = GetStandardStatusItem ( key, data );
-
- if ( !result )
- result = fMailGateway->GetStatusItem ( key, data );
-
- if ( result )
- {
- err = theMessage.WriteParameter ( id, data.GetDataType(), data.GetPhysicalStart (), data.GetPhysicalLength () );
- if (steveFlag.Flag(3)) {
- OutputOSType( steve, id); steve << " ";
- OutputOSType( steve, data.GetDataType() );
- steve << " (" << data.GetPhysicalLength () << ") ";
- }
- }
- }
-
- steveF ( 3, "." );
-
- theMessage.Send(err);
- }
-
- index--;
- }
-
- #if debug
- TSleep ( steveFlag.Flag ( 4 ) ? 300 : 1800 );
- #else
-
- TSleep ( 60 * 30 );
-
- #endif
- }
- }
-
- /***********************************|****************************************/
-